package com.guang.springbootmybatisbatchoperate.servcie.impl;

import com.guang.springbootmybatisbatchoperate.batch.MapperBatchExecutor;
import com.guang.springbootmybatisbatchoperate.dao.UserMapper;
import com.guang.springbootmybatisbatchoperate.pojo.User;
import com.guang.springbootmybatisbatchoperate.servcie.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author liguang
 * @date 2022/6/14 14:54
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MapperBatchExecutor mapperBatchExecutor;

    /**
     * 因为使用的不多，直接从配置文件中获取得到接口
     */
    @Value("${batchstatement.insertuser}")
    private String batchInsert2UserStatement;

    @Override
    public void batchInsert() {
        ArrayList<User> users = new ArrayList<>(5000);
        for (int i = 0; i < 5000; i++) {
            User user = new User();
            user.setAge(i + 1);
            user.setName(user.getAge().toString());
            users.add(user);
        }
        long startTime = System.currentTimeMillis();
        int i = mapperBatchExecutor.batchInsert(batchInsert2UserStatement, users);
        long endTime = System.currentTimeMillis();
        // 插入数据成功，耗时：1742，插入数据有：5000
        System.out.println(String.format("插入数据成功，耗时：%s，插入数据有：%s", (endTime - startTime), i));
    }
}
